<?php

namespace app\admin\model;

use addons\shopro\model\Cats;
use think\Model;


class CatOrder extends Model
{


    // 表名
    protected $name = 'shopro_cats_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'pay_type_text',
        'createtime_text',
        'paytime_text',
        'cycletime_text',
    ];


    public function getStatusList()
    {
        return ['-2' => __('交易关闭'), '-1' => __('已取消'), '0' => __('待签约'), '1' => __('已签约'), '2' => __('已完成'),
            '3' => __('已解约'), '4' => __('扣款成功'),'6' => __('扣款失败'), '5' => __('已退款')];
    }

    public function getPayTypeList()
    {
        return ['wechat' => __('Pay_type wechat'), 'alipay' => __('Pay_type alipay'), 'wallet' => __('Pay_type wallet'), 'score' => __('Pay_type score')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
        $list = $this->getPayTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }
    public function getCycletimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['cycletime']) ? $data['cycletime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }
    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPaytimeTextAttr($value)
    {
        return $value === '' ? '未支付' :  date("Y-m-d H:i:s", $value) ;
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id',[], 'LEFT')->setEagerlyType(0);
    }
    public function cat()
    {
        return $this->belongsTo(Cats::class, 'cat_id', 'id',[], 'LEFT')->setEagerlyType(0);
    }
//    public function cat()
//    {
//        return $this->hasOne(Cats::class, 'id', 'cat_id');
//    }
}
